package shopping;

import java.util.Scanner;

public class ShoppingCard {
    public static void main(String[] args) {
        //创建商品数组
        product[] arr=new product [5];
        //创建商品对象
        product p1=new product("雪碧","饮料",4,40);
        product p2=new product("苹果","水果",2,80);
        product p3=new product("可乐","饮料",4,40);
        product p4=new product("果汁","饮料",6,50);
        product p5=new product("香蕉","水果",5,30);
        
        //把商品对象加入商品数组中
        arr[0]=p1;
        arr[1]=p2;
        arr[2]=p3;
        arr[3]=p4;
        arr[4]=p5;
       // System.out.println(arr[1].getPrice());
        //遍历数组
            //用户操作
            //用户选择商品
        String shCar="";
        product[] cart=new product[5];
        product[] newcart=new product[5];
        int count=0;
            while(true) {
                System.out.println("请输入商品名称，输入null结束输入");
                Scanner sc = new Scanner(System.in);
                String str=sc.next();
                if(str.equals("null")){
                    break;
                }
                boolean found=false;
                for (int i = 0; i < arr.length; i++) {
                    product pro = arr[i];
                    if (pro.getBrand().equals(str)) {
                        System.out.println(pro.getBrand() + "，" + pro.getType() + "，" + pro.getPrice() + "," + pro.getNum());
                        System.out.println("请输入商品个数：");
                        int number= sc.nextInt();
                        if(number<=arr[i].getNum()){
                            System.out.println("添加成功");
                            if(count>20){
                                System.out.println("购物车已满");
                                break;
                            }
                        }
                        else if(number>arr[i].getNum()) {
                            System.out.println("商品库存不够");
                        }
                        found=true;
                        shCar=shCar+pro.getBrand()+","+number+"个  ";
                        for (int n = 0; n < cart.length; n++){
                            product c=new product(str,pro.getType(),pro.getPrice(),number);
                            cart[n]=c;
                        }
                        count ++;
                        break;
                    }
                }
                    if (!found) {
                        System.out.println("商品名称不存在");
                    }
        }
            //创建一个购物车
        //String shCar="";
        System.out.println("现在的购物车中有以下商品");
        System.out.println(shCar);
        //遍历购物车数组
        for (int n = 0; n < count; n++) {
            product pro2 = cart[n];
            //System.out.println(pro2.getBrand()+","+pro2.getType()+","+pro2.getPrice()+","+pro2.getNum());
            System.out.println("如果要删除商品请输入1，要结算输入2");
            Scanner sc=new Scanner(System.in);
            int cartnum = sc.nextInt();
            if(cartnum==1){
                System.out.println("请输入你要删除的商品名称");
                Scanner name=new Scanner(System.in);
                String cartName = name.next();
                if(pro2.getBrand().equals(cartName)) {
                    boolean check=false;
                    if(pro2.getBrand().equals(cartName)){
                        check=true;
                        break;
                    }
                    else{//(删除商品)
                        //for (int k = 0; k < newcart.length-1; k++){
//product c2=new product(pro2.getBrand()+","+pro2.getType()+","+pro2.getPrice()+","+pro2.getNum());
                        //    newcart[n]=c2;
                        }
                    }
                    System.out.println("删除成功");
                }
            else if(cartnum==2){
                int sum=0;
                for(int i=0;i<count;i++){
                    sum+=cart[i].getNum()* cart[i].getPrice();//(有问题)
                }
                System.out.println(sum);
                break;
            }
            }

        }


    }

